<?php
/*
 *
 * 孙阳
 * 2017/1/18
 * copy from 品尚汇接口
 *
 */
class Server
{
    protected $m = 'default';  //模块
    protected $a = 'index';	//操作

    public function __construct()
    {
        $this->init();

    }

    private function init()
    {
        $url = parse_url($_SERVER['REQUEST_URI']);
        $param = explode('/', trim(str_replace('/index.php','',$url['path']), '/'));
        if(!empty($param[0])){
            $this->m = $param[0];
        }
        if(!empty($param[1])){
            $this->a = $param[1];
        }

    }

    public function run()
    {
        $apiFile = APPPATH.'/controller/' . $this->m . '.controller.php';
        if (file_exists($apiFile))
        {
            require_once $apiFile;


            $model = ucfirst($this->m) .'Controller';
            $action = $this->a;
            $apiServer = new $model;
            if(method_exists($apiServer,$action)){
                $apiServer->$action();
            }else{
                $this->render('110','the act url is not exists!');
            }


        }else{
            $this->render('110','the app url is not exists!');

        }
    }

    public function render ($code, $message, $result = '')
    {
        echo json_encode(array(
            'code'		=> $code,
            'message'	=> $message,
            'result'	=> $result
        ));
        exit;
    }


}

?>